package ext.acae2.dms.parse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import wt.log4j.LogR;
import ext.acae2.dms.bean.EPMBuildRuleBean;
import ext.acae2.dms.bean.EPMDocumentBean;
import ext.acae2.dms.bean.EPMMemberLinkBean;
import ext.acae2.dms.bean.PartBean;
import ext.acae2.dms.bean.PartUsageLinkBean;
import ext.acae2.util.ParameterUtil;

public class ParsePartUsageLink {

	private static final Logger LOGGER = LogR
			.getLogger(ParsePartUsageLink.class.getName());

	public static List<PartUsageLinkBean> execute(List<PartBean> partList) {

		LOGGER.debug("<<< Start parsePartUsageLink");

		List<PartUsageLinkBean> partLinkList = new ArrayList<PartUsageLinkBean>();

		for (int i = 0; i < partList.size(); i++) {
			PartBean childPartBean = partList.get(i);
			String parentIds = childPartBean.getParentId();
			if (parentIds != null) {

				String[] parentIdArr = parentIds.split(",");

				for (int j = 0; j < partList.size(); j++) {
					PartBean parentPartBean = partList.get(j);

					/** after method begin */
					int n = 0;
					for (String parentId : parentIdArr) {
						if (parentId.indexOf(parentPartBean.getId()) > -1) {
							n++;
						}
					}
					for (int k = 0; k < n; k++) {
						PartUsageLinkBean partLink = new PartUsageLinkBean(
								parentPartBean, childPartBean);
						partLinkList.add(partLink);
					}

					/** before method begin */
					// if(childPartBean.getParentId().equals(parentPartBean.getId())){
					// PartUsageLinkBean partLink = new PartUsageLinkBean(
					// parentPartBean, childPartBean);
					// partLinkList.add(partLink);
					// }

				}
			}
		}

		LOGGER.debug("<<< Start parsePartUsageLink");
		return partLinkList;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void excute(Map<String,List> dataMap){
		
		List<PartBean> partList = dataMap.get(ParameterUtil.PARTBEAN);
		List<EPMBuildRuleBean> epmBuildRuleList = dataMap.get(ParameterUtil.EPMBUILDRULELINK);
		
		List<PartUsageLinkBean> partLinkList = new ArrayList<PartUsageLinkBean>();
		List<EPMMemberLinkBean> epmMemberList = new ArrayList<EPMMemberLinkBean>();
		for (int i = 0; i < partList.size(); i++) {
			PartBean childPartBean = partList.get(i);
			String parentIds = childPartBean.getParentId();
			if (parentIds != null) {

				String[] parentIdArr = parentIds.split(",");

				for (int j = 0; j < partList.size(); j++) {
					PartBean parentPartBean = partList.get(j);

					/** after method begin */
					int n = 0;
					for (String parentId : parentIdArr) {
						if (parentId.indexOf(parentPartBean.getId()) > -1) {
							n++;
						}
					}
					/**usagelink constructor begin*/
					if(n>0){
						PartUsageLinkBean partLink = new PartUsageLinkBean(
								parentPartBean, childPartBean);
						partLink.setSingleAmout(n*1.0);
						partLinkList.add(partLink);
						EPMDocumentBean parentEpmBean =null;
						EPMDocumentBean childEpmBean =null;
						for(EPMBuildRuleBean epmBuildRule:epmBuildRuleList){
							if(parentPartBean.getId().equals(epmBuildRule.getPart().getId())){
								parentEpmBean = epmBuildRule.getEpmDocument();
							}
							if(childPartBean.getId().equals(epmBuildRule.getPart().getId())){
								childEpmBean = epmBuildRule.getEpmDocument();
							}
							if(parentEpmBean!=null&&childEpmBean!=null){
								parentEpmBean.setType("ASSEMBLY");
								EPMMemberLinkBean memberLink = new EPMMemberLinkBean();
								memberLink.setParentEpm(parentEpmBean);
								memberLink.setChildEpm(childEpmBean);
								memberLink.setSingleAmout(1.0);
								memberLink.setMemberCount(n);//memberLink 数量
								epmMemberList.add(memberLink);
								epmBuildRule.setParentPart(parentPartBean);
								epmBuildRule.setParentEpmBean(parentEpmBean);
								
								//add by cnie for MemberLink chongfu 20130712
								parentEpmBean =null;
								childEpmBean =null;
							}
						}
					}
					
					/**usagelink constructor end*/
				}
			}
		}
		System.out.println("epmMemberList===============>"+epmMemberList.size());
		
		System.out.println("partLinkList===============>"+partLinkList.size());
		dataMap.put(ParameterUtil.EPMMEMBERLINK, epmMemberList);
		dataMap.put(ParameterUtil.PARTUSAGELINKBEAN, partLinkList);
	}
}
